
<%@ page import="java.util.*"%>
<%@ page import="Domain.DataManager"%>
<%@ page import="Domain.UserClient.ButtonsFunctions"%>
<%@ page import="Domain.UserClient.SocketListener"%>
<%@ page import="Domain.stompProtocol.StompClient"%>
<%@ page import="javax.annotation.PreDestroy"%>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>

<%
	DataManager dm = (DataManager) (session.getAttribute("dm"));
	if (dm == null)
		response.sendRedirect("Welcome.jsp");
	else if (dm.getUserStatus() == DataManager.CONNECTED) {
		response.sendRedirect("ChooseForum.jsp");
	} else {
%>

<%
	dm.setCurrForum(null);
		dm.setCurrThread(null);
		dm.setCurrUser("");
%>

<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" href="style.css" type="text/css">

<!---the title of the page-->
<title>Register Page</title>

<script language="javascript" type="text/javascript">
<!--
	//Browser Support Code
	function updateAvailability() {
		var ajaxRequest;

		// The variable that makes Ajax possible! 
		try {
			// Opera 8.0+, Firefox, Safari 
			ajaxRequest = new XMLHttpRequest();
		} catch (e) {
			// Internet Explorer Browsers 
			try {
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					// Something went wrong 
					alert("Your browser broke!");
					return false;
				}
			}
		}

		// Create a function that will receive data sent from the server 
		ajaxRequest.onreadystatechange = function() {
			if (ajaxRequest.readyState == 4) {
				var response = ajaxRequest.responseText;
				document.getElementById("availableLabel").innerHTML = response;
			}
		};

		var url = "CheckAvailability.jsp";
		var params = "userName="
				+ encodeURI(document.getElementById('username').value);
		ajaxRequest.open("POST", url, false);

		//Send the proper header information along with the request
		ajaxRequest.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded");
		ajaxRequest.setRequestHeader("Content-length", params.length);
		ajaxRequest.setRequestHeader("Connection", "close");

		ajaxRequest.send(params);
	}
// -->
</script>

<script language="javascript" type="text/javascript">
<!--
	function checkNotEmpty() {
		var userName = document.myForm.username.value;
		var password = document.myForm.password.value;
		if (password == "" | userName == "")
			document.getElementById("errorLabel").innerHTML = "<font color=\"red\">you have to enter both username and password</font>";
		else if (userName.indexOf('&') != -1 | password.indexOf('&') != -1)
			document.getElementById("errorLabel").innerHTML = "<font color=\"red\">you can't enter the char '&'</font>";
		else
			parent.location = "CheckRegister.jsp?username=" + userName + "&password=" + password;
	}
// -->
</script>

</head>

<body>

	<table width="100%" height="100%" border="1" cellspacing="0"
		cellpadding="0">

		<!-- HEADER -->
		<tr class="header">
			<td>
				<!-- cool navigation toolbar code -->
				<ul id="navlist">
					<li id="prev"><a href="Welcome.jsp"></a>
					</li>
					<!-- <li id="notifications"><a href="ChooseForum.jsp"></a></li> -->
					<li id="connection">Connected as: <b><i><font
								color='#FF0000'>guest</font> </i> </b>
					</li>
				</ul>
			</td>
		</tr>

		<!-- BODY -->
		<tr class="body">
			<td class="forumBody">
				<center>
					<h6>
						<font color="#FFF11" size="7"><i>Registration</i> </font>
					</h6>
				</center>

				<FORM METHOD=POST ACTION="CheckRegister.jsp" name="myForm">
					<div align="center">
						<br> <br> <br> <b><font color='yellow'> Enter username: <INPUT
							TYPE=TEXT NAME='username' id='username' SIZE=20 value="">
							</font></b>
						<br> <br>
						<button type="button" onClick="updateAvailability();">Check
							if available</button>
						<p id='availableLabel'>
							<font color="red"></font>
						</p>
						<b><font color='yellow'>
						Enter Password: <INPUT TYPE=password NAME='password' SIZE=20
							value=""> </font></b><br> <br> <br>
						
						<p id='errorLabel'></p>
						
						<P>
							<button type="button" onClick="checkNotEmpty();">Submit</button>
						</P>
						
						<br><b><font color="red">Some error occur, try to check
							whether your username is available</font></b>
					</div>
				</FORM></td>
	</table>
</body>
</html>

<%
	}
%>



